<?php

class Technical_TypecreditController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $typeCredits = new Application_Model_Dao_TypeCredit();
        $result = $typeCredits->getAll()->toArray();
        $this->view->typeCredits = $result;
    	if($result == null){
			$this->view->empty = "Brak rodzajów zaliczenia w bazie.";
		}else{
			$this->view->empty = "";
		}
        $session = new Zend_Session_Namespace('identity');
		$designation = $this->_getParam('designation');
        if(isset($session->statusdel) && $this->_getParam('statusdelete') == 'true') {
        	$this->view->delete = "Rekord \"".$designation."\" został poprawnie usunięty.";
			unset($session->statusdel);	
        }else{
        	$this->view->delete = "";
        }
    }

    public function addAction(){
    	
    	$form = new Application_Form_TypeCredit();
    	$form->appendValidationEngineFiles($this->view); 
    	$polish = BH_TranslatePL::getPolishTranslation();
  		$translate = new Zend_Translate('array', $polish, 'pl');
  		$form->setTranslator($translate);
    	
    	$form->submit->setLabel('Dodaj nowy rodzaj zaliczenia');
		$this->view->TypeCreditForm = $form;
   	
    	$request = $this->getRequest();
    	if ($request->isPost()) {
    		if ($form->isValid($request->getPost())) {
    			$typeCredit = new Application_Model_Entities_TypeCredit();
    			$typeCredit->setDesignation( $form->getValue('designation'));
    			$typeCreditDao = new Application_Model_Dao_TypeCredit();
    			$typeCreditDao->saveTypeCredit($typeCredit);
    		
    			$this->_helper->redirector('index');		
    		}
    	}
    }
  	public function editAction(){
		 
		$form = new Application_Form_TypeCredit();
		$form->appendValidationEngineFiles($this->view);
		$polish = BH_TranslatePL::getPolishTranslation();
		$translate = new Zend_Translate('array', $polish, 'pl');
		$form->setTranslator($translate);
		 
		$form->submit->setLabel('Edytuj rodzaj zaliczenia');
		$this->view->TypeCreditEditForm = $form;

		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$typeCredit = new Application_Model_Entities_TypeCredit();
				$typeCredit->setId( $form->getValue('id'));
				$typeCredit->setDesignation( $form->getValue('designation'));
				$typeCreditDao = new Application_Model_Dao_TypeCredit();
				$typeCreditDao->updateTypeCredit($typeCredit);

				$this->_helper->redirector('index');
			} else {
				$form->populate($formData);
			}
		}else{
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$typeCreditDao = new Application_Model_Dao_TypeCredit();
				$form->populate($typeCreditDao->getTypeCreditByID($id));
			}
		}
	}
public function deleteAction(){

		$request = $this->getRequest();
		if ($request->isPost()) {
			$del = $request->getPost('del');
			if ($del == 'Tak, usuń rekord') {
				$id = $request->getPost('id');
				$designation = $request->getPost('designation');
				$typeCredit = new Application_Model_Entities_TypeCredit();
				$typeCredit->setId($id);
				$typeCreditDao = new Application_Model_Dao_TypeCredit();
				$typeCreditDao->deleteTypeCredit($typeCredit);
				$session = new Zend_Session_Namespace('identity');
    			$session->statusdel = 'true';
				$this->_helper->redirector( 'index', 'typecredit', 'technical', array('statusdelete' => 'true', 'designation' => $designation));
			}else
				$this->_helper->redirector( 'index');
		}
		if($this->_getParam('jsflag') == 'false' ){
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$typeCreditDao = new Application_Model_Dao_TypeCredit();
				$result = $typeCreditDao->getRowTypeCredit($id);
				$this->view->typeCredits = $result;
			}
		}
		
		if($this->_getParam('jsflag') == 'true' ){
			$id = $this->_getParam('id', 0);
				$typeCredit = new Application_Model_Entities_TypeCredit();
				$typeCredit->setId($id);
				$typeCreditDao = new Application_Model_Dao_TypeCredit();
				$typeCreditDao->deleteTypeCredit($typeCredit);
			//$this->_helper->redirector('index');
		}
	}
    
}
